﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

using Jungle.Rent;
using Jungle.Code.Utils;
using Jungle.Code.Core;

namespace PRVIgroup.Modules.OfferManager.Controls
{
    public partial class ShowOffer : System.Web.UI.UserControl
    {
        private Language lang;
        private string[] langParams;
        XmlDocument langDoc = new XmlDocument();
        // -------------------------------------------------------------------------------
        protected override void Render(HtmlTextWriter writer)
        {
            LoadLanguage();
            if (Request["id"] != null) GetContent(writer, new Offer(Convert.ToInt32(Request["id"])));
            base.Render(writer);
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter GetContent(HtmlTextWriter writer, Offer o)
        {
            writer.AddAttribute("class", "offer_Table");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
                GetTr(writer, langParams[7], Offer.ddl_ObjectType().Items.FindByValue(o.ObjectTypeId.ToString()).Text);
                GetTr(writer, langParams[1], Offer.ddl_Countrys().Items.FindByValue(o.CountryId.ToString()).Text);
                if (o.City.Length > 0) GetTr(writer, langParams[8], o.City);
                if (o.Adress.Length > 0) GetTr(writer, langParams[9], o.Adress);
                if (o.Rooms > 0) GetTr(writer, langParams[10], o.Rooms.ToString());
                if (o.GroundTax > 0) GetTr(writer, langParams[11], o.GroundTax.ToString());
                if (o.Price > 0) GetTr(writer, langParams[12], o.Price.ToString() + " &#x20AC;");
                if (o.Tax > 0) GetTr(writer, langParams[13], o.Tax.ToString() + " &#x20AC;");
                if (o.Floor > 0 && o.Floors > 0) GetTr(writer, langParams[14] +"/" + langParams[15], o.Floor.ToString() +"/"+ o.Floors.ToString());
                if (o.Floor > 0 && o.Floors == 0) GetTr(writer, langParams[14], o.Floor.ToString());
                if (o.Floor == 0 && o.Floors > 0) GetTr(writer, langParams[15], o.Floors.ToString());
                if (o.SquareTotal > 0) GetTr(writer, langParams[16] + " m<sup>2</sup>", o.SquareTotal + "/" + o.SquareLive + "/" + o.SquareKit);
                if (o.AreaSquare > 0) GetTr(writer, langParams[17], o.AreaSquare.ToString());
                if (o.Balcon > 0) GetTr(writer, langParams[18], o.Balcon.ToString());
                if (o.Bathroom > 0) GetTr(writer, "WC", o.Bathroom.ToString());
                if (o.Bedroom > 0) GetTr(writer, langParams[19], o.Bedroom.ToString());
                if (o.Kitchen > 0) GetTr(writer, langParams[20], o.Kitchen.ToString());
                if (o.ToSea.Length > 0) GetTr(writer, langParams[21], o.ToSea);

                string options = "";
                if (o.Laundry) options += langParams[22] + ", ";
                if (o.Storage) options += langParams[23] + ", ";
                if (o.Condition) options += langParams[24] + ", ";
                if (o.Farniture) options += langParams[25] + ", ";
                if (o.Heating) options += langParams[26] + ", ";
                if (o.Parking) options += langParams[27] + ", ";
                if (o.SeaPanarama) options += langParams[28] + ", ";
                if (o.Garden) options += langParams[29] + ", ";
                if (o.Barbeku) options += langParams[30] + ", ";
                if (o.Pool) options += langParams[31] + ", ";
                if (o.Sallon) options += langParams[32] + ", ";
                if (options.Length > 5)
                {
                    options = options.Substring(0, options.Length - 2);
                    GetTr(writer, langParams[33], options);
                }
                if (o.Contacts.Length > 0) GetTr(writer, langParams[34], o.Contacts);

                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute("colspan", "2");
                writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingTop, "15px");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("<strong>" + langParams[35] + ": </strong>" + o.Description);
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute("colspan", "2");
                writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingTop, "15px");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                if (o.Photos.Length > 5)
                {
                    string[] pp = o.Photos.Split(',');
                    if (pp.Length > 1)
                        for (int i = 1; i < pp.Length; i++) PrintPhoto(writer, pp[i]);
                }
                writer.RenderEndTag();
                writer.RenderEndTag();



                

                
            

            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter GetTr(HtmlTextWriter writer, string name, string value)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("<strong>"+name+": </strong>");
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(ChangeParameterDdl(value));
                writer.RenderEndTag();
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintPhoto(HtmlTextWriter writer, string imgUrl)
        {
            int _width = 225;
            int _heigth = 0;

            System.Drawing.Image loadedImg = System.Drawing.Image.FromFile(Server.MapPath(imgUrl));

            if (loadedImg.Width < _width) _width = loadedImg.Width;

            double d1 = Math.Round((double)loadedImg.Width / _width, 4);
            double d2 = Math.Round((double)loadedImg.Height / d1, 4);
            _heigth = Convert.ToInt32(d2);

            writer.AddStyleAttribute("float", "left");
            writer.AddStyleAttribute("margin", "10px");
            writer.AddStyleAttribute("cursor", "pointer");
            writer.AddAttribute("onclick", "Show(this);");
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, _width.ToString());
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, _heigth.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        // -----------------------------------------------------------------------------------------------
        private void LoadLanguage()
        {
            string ControlPath = "/Modules/OfferManager";
            int LangId = Node.GetByUrl(UrlHelper.GetRequestUrl().Substring(0, UrlHelper.GetRequestUrl().IndexOf(".aspx")).Replace("/main", "")).LangID;
            Language l = new Language(LangId, "", "", true, 0); l.GetById();
            lang = l;

            langDoc.Load(Context.Server.MapPath(ControlPath + "/langs/" + l.Name + ".xml"));
            langParams = new string[langDoc.ChildNodes[1].ChildNodes.Count];
            for (int i = 0; i < langDoc.ChildNodes[1].ChildNodes.Count; i++)
            {
                langParams[i] = langDoc.ChildNodes[1].ChildNodes[i].InnerText;
                
            }
        }
        private string ChangeParameterDdl(string input)
        {
            string output = input;
            foreach(XmlNode n in langDoc.ChildNodes[1].ChildNodes)
            {
                if (n.Name == input) output = n.InnerText;
            }
            return output;
        }
    }
}